/**
 * 
 */
package com.yidu.erp.action.BeyondStaff;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yidu.common.ExcelUtil;
import com.yidu.common.Pages;
import com.yidu.common.SsmMessage;
import com.yidu.erp.model.BeyondModel;
import com.yidu.erp.model.BeyondStaff;
import com.yidu.erp.service.BeyondModel.ModelService;
import com.yidu.erp.service.BeyondPersonelRole.PersonelRoleService;
import com.yidu.erp.service.BeyondRole.RoleService;
import com.yidu.erp.service.BeyondRoleModel.RoleModelService;
import com.yidu.erp.service.BeyondStaff.StaffService;

/**
 * 员工Controller
 * @author wws
 * @date  2017年10月26日
 */
@Controller
@RequestMapping("staff")
public class BeyondStaffAction {
	
	@Resource
	private StaffService staffService ;//员工service
	
	@Resource
	private PersonelRoleService personelRoleService;//员工角色service
	
	@Resource
	private RoleService roleService ;//角色service
	
	@Resource
	private RoleModelService roleModelService;//角色模块service
	
	@Resource
	private ModelService modelService ;//模块service
	
	/**
	 * 显示列表
	 * @author zengren
	 * @param paramMap
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("showList")
	@ResponseBody
	public Map<String, Object> showList(@RequestParam Map<String, Object> paramMap, int page, int limit){
		Pages pages = new Pages();
		pages.setCurPage(page);
		pages.setMaxResult(limit);
		paramMap.put("pages", pages);
		return staffService.showList(paramMap);
	}
	
	/**
	 * 登录
	 * @author zengren
	 * @param paramMap
	 * @param session
	 * @return
	 */
	@RequestMapping("isLogin")
	@ResponseBody
	public SsmMessage isLogin(@RequestParam Map<String, Object> paramMap, HttpSession session){
		SsmMessage mse = new SsmMessage();
		
		BeyondStaff staff = staffService.isLogin(paramMap);
		
		if(staff!=null){
			session.setAttribute("staff", staff);
			mse.setMes(SsmMessage.SUCCESS_MES);
			mse.setState(SsmMessage.SUCCESS);
		}else{
			mse.setMes(SsmMessage.ERROR_MES);
			mse.setState(SsmMessage.ERROR);
		}
		return mse;
	}
	
	/**
	 * 增加或修改
	 * @author zengren
	 * @param staff 人员对象
	 * @return
	 */
	@RequestMapping("addOrUpdate")
	@ResponseBody
	public SsmMessage addOrUpdate(BeyondStaff staff){
		SsmMessage mse = new SsmMessage();
		
		int rows = staffService.addOrUpdate(staff);
		
		if(rows>0){
			mse.setMes(SsmMessage.SUCCESS_MES);
			mse.setState(SsmMessage.SUCCESS);
		}else{
			mse.setMes(SsmMessage.ERROR_MES);
			mse.setState(SsmMessage.ERROR);
		}
		
		return mse;
	}
	
	/**
	 * 删除
	 * @author zengren
	 * @param staffId 人员Id
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public SsmMessage delete(String staffId){
		SsmMessage mse = new SsmMessage();
		
		int rows = staffService.delete(staffId);
		
		if(rows>0){
			mse.setMes(SsmMessage.SUCCESS_MES);
			mse.setState(SsmMessage.SUCCESS);
		}else{
			mse.setMes(SsmMessage.ERROR_MES);
			mse.setState(SsmMessage.ERROR);
		}
		
		return mse;
	}
	
	/**
	 * 根据id查询
	 * @author zengren
	 * @param staffId 人员id
	 * @return 
	 */
	@RequestMapping("findById")
	@ResponseBody
	public BeyondStaff findById(String staffId){
		return staffService.findById(staffId);
	}
	
	/**
	 * 把上传的Excel导入到数据库
	 * @author zengren
	 * @param file
	 * @return
	 */
	@RequestMapping(value="uploadExcel",method = RequestMethod.POST)  
    @ResponseBody  
    public SsmMessage uploadExcel(@RequestParam(value="file",required = false)MultipartFile file){  
		SsmMessage mes = new SsmMessage();
		String result = staffService.readExcelFile(file);
        if("1".equals(result)){
        	mes.setMes(SsmMessage.SUCCESS_MES);
        	mes.setState(SsmMessage.SUCCESS);
        }else{
        	mes.setMes(SsmMessage.ERROR_MES);
        	mes.setState(SsmMessage.ERROR);
        }
        return mes;  
    }
	
	/**
	 * 将当前列表的数据导出为Excel
	 * @author zengren
	 * @param jsonStr
	 * @param response
	 */
	@RequestMapping(value="downExcel",method = RequestMethod.POST)
	public void downExcel(String jsonStr, HttpServletResponse response){
		JSONObject json = JSONObject.parseObject(jsonStr);  
		JSONArray jsonArray = JSONArray.parseArray(json.getString("data"));
		Map<String, String> headMap = new LinkedHashMap<String, String>();
		headMap.put("departName", "部门名称");
		headMap.put("annexName", "分店名称");
		headMap.put("staffAccount", "登录账号");
		headMap.put("staffDizhi", "地址");
		headMap.put("staffName", "员工姓名");
		headMap.put("staffPhone", "手机号");
		headMap.put("staffPost", "职业");
		headMap.put("staffPwd", "密码");
		headMap.put("staffRemar", "备注");
		ExcelUtil.downloadExcelFile("员工列表", headMap, jsonArray, response);
	}
	
	
	/**
	 * 首页显示
	 * @param staff
	 * @param map
	 * @return
	 */
	@RequestMapping("/indextwo")
	@ResponseBody
	public List<BeyondModel> indextwo(String staffId){
		
		return modelService.showIndex(staffId);
	}
	
	/**
	 * 退出登录
	 * @return
	 */
	@RequestMapping("logout")
	public ModelAndView logout(HttpSession session){
		ModelAndView mav = new ModelAndView("redirect://pages/login.jsp");
		return mav;
	}
	
}
